﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.Common;
using MySql.Data.MySqlClient;
using MaxLeaf.DB;

namespace MaxLeaf.DAL.Web
{
    /// <summary>
    /// 商品分类表
    /// </summary>
    public class mary_categoryDAL
    {
        public string mstrItemFNumber = string.Empty;
        public string Add(DBAccess dbCon, string strParent_Id, string strFItemID, string strCat_Name)
        {
            string strSql = "insert into mary_category(Cat_Name,parent_id,FItemID) values(?Cat_Name,?parent_id,?FItemID);select  @@identity";
            MySqlParameter[] arrParam = new MySqlParameter[] { 
                new MySqlParameter("?Cat_Name",strCat_Name) 
                ,new MySqlParameter("?parent_id", strParent_Id) 
                ,new MySqlParameter("?FItemID", strFItemID) 
            };

            string strCat_Id = dbCon.GetValue(strSql, arrParam);
            UpdateFItemId(dbCon, strCat_Id, strFItemID);
            return strCat_Id;

        }


        public string GetCat_id_ByName(DBAccess dbCon, string strParentCat_id, string strCat_Name)
        {
            DataRow drData = null;
            if (strParentCat_id.Equals("0"))
            {
                string strSql = "select Cat_Id from mary_category where Cat_Name=?Cat_Name and  parent_id in (select Cat_Id from mary_category where parent_id=0) ";
                drData = dbCon.GetDataFirstRow(strSql, new MySqlParameter[] { new MySqlParameter("?Cat_Name", strCat_Name) });

            }
            else
            {
                drData = dbCon.GetTableDataFirstRow("mary_category", new Dictionary<string, object> { { "Cat_Name", strCat_Name }, { "parent_id", strParentCat_id } }, "Cat_Id");

            }
            if (drData != null)
            {
                return CvtFunc.ToString(drData["Cat_Id"]);
            }
            //不能添加第一级分类
            if (strParentCat_id.Equals("0"))
            {
                throw new MaxLeafException(string.Format("mary_category 不存在 {0} for Fnumber {1}", strCat_Name, mstrItemFNumber));
            }
            return Add_ByName(dbCon, strParentCat_id, strCat_Name);
            
        }

        public string Add_ByName(DBAccess dbCon, string strParent_Id, string strCat_Name)
        {
            string strSql = "insert into mary_category(Cat_Name,parent_id) values(?Cat_Name,?parent_id);select  @@identity";
            MySqlParameter[] arrParam = new MySqlParameter[] { 
                new MySqlParameter("?Cat_Name",strCat_Name) 
                ,new MySqlParameter("?parent_id", strParent_Id) 

            };
            return dbCon.GetValue(strSql, arrParam);

        }
        public string GetCat_id(DBAccess dbOrgCon, DBAccess dbCon, string strParentCat_id, string strFNumber)
        {
            DataRow drData = null;
            string strSql = string.Empty;
            bool blnNeedUpdateFitemId = false;

            DataRow drOrgT_Item = dbOrgCon.GetTableDataFirstRow("T_Item", new Dictionary<string, object> { { "fnumber", strFNumber }, { "FItemClassID", "4" } }, "FName,FItemID");

            if (strParentCat_id.Equals("0"))
            {
                strSql = "select Cat_Id from mary_category where FItemID=?FItemID and  parent_id in (select Cat_Id from mary_category where parent_id=0) ";
                drData = dbCon.GetDataFirstRow(strSql, new MySqlParameter[] { new MySqlParameter("?FItemID", drOrgT_Item["FItemID"]) });

                if (drData == null)
                {
                    blnNeedUpdateFitemId = true;
                    strSql = "select Cat_Id from mary_category where Cat_Name=?Cat_Name and  parent_id in (select Cat_Id from mary_category where parent_id=0) ";
                    drData = dbCon.GetDataFirstRow(strSql, new MySqlParameter[] { new MySqlParameter("?Cat_Name", drOrgT_Item["FName"]) });
                }
            }
            else
            {
                strSql = "select Cat_Id from mary_category where FItemID=?FItemID and  parent_id<>0 ";
                drData = dbCon.GetDataFirstRow(strSql, new MySqlParameter[] { new MySqlParameter("?FItemID", drOrgT_Item["FItemID"]) });
                if (drData == null)
                {
                    blnNeedUpdateFitemId = true;
                    drData = dbCon.GetTableDataFirstRow("mary_category", new Dictionary<string, object> { { "Cat_Name", drOrgT_Item["FName"] }, { "parent_id", strParentCat_id } }, "Cat_Id");
                }
            }
            if (drData != null)
            {
                strSql = "update mary_category set Cat_Name=?Cat_Name where Cat_Id=?Cat_Id   ";
                dbCon.ExecuteSql(strSql, new MySqlParameter[] { new MySqlParameter("?Cat_Name", drOrgT_Item["FName"]), new MySqlParameter("?Cat_Id", drData["Cat_Id"]) });
                if (!strParentCat_id.Equals("0"))
                {
                    strSql = "update mary_category set parent_id=?parent_id where Cat_Id=?Cat_Id   ";
                    dbCon.ExecuteSql(strSql, new MySqlParameter[] { new MySqlParameter("?parent_id", strParentCat_id), new MySqlParameter("?Cat_Id", drData["Cat_Id"]) });
                }
                if (blnNeedUpdateFitemId)
                {
                    UpdateFItemId(dbCon, CvtFunc.ToString(drData["Cat_Id"]), CvtFunc.ToString(drOrgT_Item["FItemID"]));


                }
                return CvtFunc.ToString(drData["Cat_Id"]);
            }
            //不能添加第一级分类
            if (strParentCat_id.Equals("0"))
            {
                throw new MaxLeafException(string.Format("mary_category 不存在 {0} for Fnumber {1}", CvtFunc.ToString(drOrgT_Item["FName"]), mstrItemFNumber));
            }
            return Add(dbCon, strParentCat_id, CvtFunc.ToString(drOrgT_Item["FItemId"]), CvtFunc.ToString(drOrgT_Item["FName"]));
        }

        private void UpdateFItemId(DBAccess dbCon, string strCat_Id, string strFItemID)
        {
            string strSql = "update mary_category set FItemID=0 where Cat_Id<>?Cat_Id and  FItemID=?FItemID ";
            dbCon.ExecuteSql(strSql, new MySqlParameter[] { new MySqlParameter("?Cat_Id", strCat_Id), new MySqlParameter("?FItemID", strFItemID) });

            strSql = "update mary_category set FItemID=?FItemID where Cat_Id=?Cat_Id   ";
            dbCon.ExecuteSql(strSql, new MySqlParameter[] { new MySqlParameter("?FItemID", strFItemID), new MySqlParameter("?Cat_Id", strCat_Id) });

        }

    }
}
